package gobelinmaker.data;

import gobelinmaker.client.Dialogs;
import gobelinmaker.client.GobelinClient;
import gobelinmaker.server.GobelinServer;
import java.awt.Component;
import java.awt.Toolkit;
import java.io.IOException;

/**
 * Alkalmazások indítását megkönnyítő osztály.
 *
 * @author imruf84
 */
public class ApplicationChooser extends javax.swing.JFrame {

    /**
     * Creates new form ApplicationChooser
     */
    public ApplicationChooser() {
        initComponents();
        this.setLocationRelativeTo(null);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        serverPanel = new javax.swing.JPanel();
        serverNameLabel = new javax.swing.JLabel();
        serverNameTextField = new javax.swing.JTextField();
        vcamerasCheckBox = new javax.swing.JCheckBox();
        vmotorsCheckBox = new javax.swing.JCheckBox();
        vlightsCheckBox = new javax.swing.JCheckBox();
        runButton = new javax.swing.JButton();
        runServerCheckBox = new javax.swing.JCheckBox();
        runClientCheckBox = new javax.swing.JCheckBox();
        jSeparator1 = new javax.swing.JSeparator();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Alkalmazás választó");
        setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getClassLoader().getResource("icons/MainFrameIcon.png")));
        setResizable(false);

        serverPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Szerver"));
        serverPanel.setEnabled(false);

        serverNameLabel.setText("Szerver neve: ");
        serverNameLabel.setEnabled(false);

        serverNameTextField.setEnabled(false);

        vcamerasCheckBox.setText("Virtuális kamerák");
        vcamerasCheckBox.setEnabled(false);

        vmotorsCheckBox.setText("Virtuális motorok");
        vmotorsCheckBox.setEnabled(false);

        vlightsCheckBox.setText("Virtuális fények");
        vlightsCheckBox.setEnabled(false);

        javax.swing.GroupLayout serverPanelLayout = new javax.swing.GroupLayout(serverPanel);
        serverPanel.setLayout(serverPanelLayout);
        serverPanelLayout.setHorizontalGroup(
            serverPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(serverPanelLayout.createSequentialGroup()
                .addComponent(serverNameLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(serverNameTextField))
            .addGroup(serverPanelLayout.createSequentialGroup()
                .addGroup(serverPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(vcamerasCheckBox, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(vmotorsCheckBox, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(vlightsCheckBox, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(0, 166, Short.MAX_VALUE))
        );
        serverPanelLayout.setVerticalGroup(
            serverPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(serverPanelLayout.createSequentialGroup()
                .addGroup(serverPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(serverNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(serverNameLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(vcamerasCheckBox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(vmotorsCheckBox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(vlightsCheckBox))
        );

        runButton.setText("Indítás");
        runButton.setEnabled(false);
        runButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                runButtonActionPerformed(evt);
            }
        });

        runServerCheckBox.setText("Szerver indítása");
        runServerCheckBox.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                runServerCheckBoxStateChanged(evt);
            }
        });

        runClientCheckBox.setText("Kliens indítása");
        runClientCheckBox.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                runClientCheckBoxStateChanged(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSeparator1)
            .addComponent(serverPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(runButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(runServerCheckBox)
                    .addComponent(runClientCheckBox))
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(runServerCheckBox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(serverPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(runClientCheckBox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(runButton))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void runServerCheckBoxStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_runServerCheckBoxStateChanged
        this.serverPanel.setEnabled(this.runServerCheckBox.isSelected());
        for (Component c : this.serverPanel.getComponents()) {
            c.setEnabled(this.runServerCheckBox.isSelected());
        }

        this.runButton.setEnabled(this.runServerCheckBox.isSelected() || this.runClientCheckBox.isSelected());
    }//GEN-LAST:event_runServerCheckBoxStateChanged

    private void runClientCheckBoxStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_runClientCheckBoxStateChanged
        this.runButton.setEnabled(this.runServerCheckBox.isSelected() || this.runClientCheckBox.isSelected());
    }//GEN-LAST:event_runClientCheckBoxStateChanged

    private void runButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_runButtonActionPerformed

        // Kliens indítása, ha szükséges.
        if (this.runClientCheckBox.isSelected()) {
            GobelinClient client = new GobelinClient();
        }

        final Component c = this;

        // Szerver indítása, ha szükséges.
        if (this.runServerCheckBox.isSelected()) {
            new Thread(new Runnable() {

                @Override
                public void run() {
                    try {
                        GobelinServer gobelinServer = new gobelinmaker.server.GobelinServer(
                                serverNameTextField.getText(),
                                vcamerasCheckBox.isSelected(),
                                vmotorsCheckBox.isSelected(),
                                vlightsCheckBox.isSelected()
                        );
                    } catch (IOException ex) {
                        Dialogs.showErrorMessageDialog(c, ex.getLocalizedMessage());
                    } catch (Exception ex) {
                        Dialogs.showErrorMessageDialog(c, ex.getLocalizedMessage());
                    }
                }
            }).start();

        }

        this.dispose();

    }//GEN-LAST:event_runButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JButton runButton;
    private javax.swing.JCheckBox runClientCheckBox;
    private javax.swing.JCheckBox runServerCheckBox;
    private javax.swing.JLabel serverNameLabel;
    private javax.swing.JTextField serverNameTextField;
    private javax.swing.JPanel serverPanel;
    private javax.swing.JCheckBox vcamerasCheckBox;
    private javax.swing.JCheckBox vlightsCheckBox;
    private javax.swing.JCheckBox vmotorsCheckBox;
    // End of variables declaration//GEN-END:variables
}
